package net.jlibrary.member.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.member.action.vo.MemberVo;
import net.jlibrary.member.service.IMemberManager;
import net.jlibrary.parking.action.vo.ParkingVo;
import net.jlibrary.parking.service.IParkingManager;

@RequestMapping("/member")
@RestController
public class MemberAction {

	@Autowired
	private IMemberManager memberManager;
	@Autowired
	private IParkingManager parkingMapper;

	@RequestMapping(value = "/memberAdminView", method = RequestMethod.GET)
	public ModelAndView memberView() {
		return new ModelAndView("/member/memberView");
	}

	// 会员列表
	@RequestMapping(value = "/memberLists", method = RequestMethod.POST)
	public Pagination memberLists(MemberVo memberVo, PaginationParam param) {
		Pagination pagination = memberManager.listsPage(memberVo, param);
		return pagination;
	}

	// 表单初始化
	@RequestMapping(value = "/memberFormInit", method = RequestMethod.GET)
	public MemberVo memberFormInit() {
		MemberVo memberVo = new MemberVo();
		return memberVo;
	}

	// 保存信息
	@RequestMapping(value = "/saveMemberForm", method = RequestMethod.POST)
	public ModelMap saveMember(@ModelAttribute("vm") MemberVo memberVo) {
		memberManager.save(memberVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 删除信息
	@RequestMapping(value = "/deleteMember", method = RequestMethod.POST)
	public ModelMap deleteMember(MemberVo memberVo) {
		memberManager.delete(memberVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 修改信息
	@RequestMapping(value = "/updateMember", method = RequestMethod.POST)
	public ModelMap updateMember(MemberVo memberVo) {
		memberManager.update(memberVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 详细信息
	@RequestMapping(value = "/detailMember", method = RequestMethod.GET)
	public MemberVo detailMember(MemberVo vo) {
		vo = memberManager.get(vo);
		return vo;
	}

	@RequestMapping(value = "/parkingJsonData", method = RequestMethod.GET)
	public List<ModelMap> parkingJsonData() {
		List<ParkingVo> parkingVos = parkingMapper.lists(new ParkingVo());
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (ParkingVo vo : parkingVos) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, vo.getPrimaryKey());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, vo.getText());
			maps.add(map);
		}
		return maps;
	}
}
